/**
 * 配置swagger文档
 */
const expressSwagger = require('express-swagger-generator');
const utils = require('../utils');
const db = require('../models');
/**
 * 生成对应的模型
 */
const generateSwaggerModel = () => {
    const typeModels = {
        ResponseMeta: {
            type: 'object',
            description: '响应信息',
            properties: {
                msg: { type: 'string', description: '响应消息' },
                status: { type: 'integer', description: '状态码 -1: 请求错误,0: 请求正常,1: 请求正常（带弹窗提示）,4: 没有登录', example: 0, enum: [-1, 0, 1, 4] },
            }
        },
        PageModel: {
            type: 'object',
            description: '分页信息',
            properties: {
                currentPage: { type: 'number', description: '当前页码' },
                pageSize: { type: 'number', description: '每页条数' },
                total: { type: 'number', description: '总条数' },
                list: { type: 'array', description: '数据列表', items: { type: 'object', properties: {} } }
            }
        }
    };
    Object.keys(db.models).forEach(model => {
        const swaggerDefinition = {
            type: 'object',
            properties: {},
        };
        Object.keys(db.models[model].rawAttributes).forEach(key => {
            const field = db.models[model].rawAttributes[key];
            const swaggerField = {
                type: mapSequelizeTypeToSwagger(field.type.key),
                description: field.comment || ``,
            };
            // 针对不同的类型进行额外处理
            if (field.allowNull === false) {
                swaggerField.required = true;
            }

            if (field.defaultValue !== undefined) {
                swaggerField.example = field.defaultValue;
            }
            // 将该字段添加到 Swagger 模型中
            swaggerDefinition.properties[key] = swaggerField;
        })
        typeModels[model + 'Model'] = swaggerDefinition;
    })
    return utils.tools.lineToHump(typeModels);
}

// 将 Sequelize 类型映射为 Swagger 类型
const mapSequelizeTypeToSwagger = (type) => {
    const typeMapping = {
        STRING: 'string',
        INTEGER: 'integer',
        BOOLEAN: 'boolean',
        FLOAT: 'number',
        DATE: 'string', // ISO-8601 日期字符串
        TEXT: 'string',
        // 这里可以根据需要添加更多类型映射
    };

    return typeMapping[type] || 'string';  // 默认类型为 string
};
const options = {
    swaggerDefinition: {
        info: {
            title: '接口文档',
            version: '1.0.0',
            description: `pure-node-serve API`
        },
        host: `${process.env.SWEG_URL}:${process.env.DEV_PORT}`,
        basePath: '/',
        produces: ['application/json', 'application/xml'],
        schemes: ['http', 'https'],
        securityDefinitions: {
            JWT: {
                type: 'apiKey',
                in: 'header',
                name: 'Authorization',
            },
            Visitor: {
                type: 'apiKey',
                in: 'header',
                name: 'Visitor-Id',
            }
        },
        // 定义模型
        definitions: generateSwaggerModel(),
    },
    route: {
        url: '/api-docs',// 打开swagger文档页面地址
        docs: '/swagger.json' // swagger文件 api
    },
    basedir: __dirname, // 项目的根目录

    files: [  // 在那个文件夹下面收集注释
        '../routes/**/*.js',
    ]
}

module.exports = function (app) {
    expressSwagger(app)(options);
};